package com.nt.train.service.context;
import com.nt.train.service.WithdrawStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author hlf
 * @version 1.0
 * @date 2022/8/5 11:10
 */
@Component
public class WithdrawContext {

    /**
     * 注入到策略接口集合
     */
    //@Resource
    @Autowired
    private List<WithdrawStrategy> strategy;

//    @Autowired
//    private Set<WithdrawStrategy> strategy;

//    @Autowired
//    private Map<String, WithdrawStrategy> strategy;

    private static Map<Integer, WithdrawStrategy> strategyMap;

    /**
     *将集合转换到map 方便过去具体的策略类
     */
    @PostConstruct
    public void init() {
        System.err.println(strategy);
        strategyMap = strategy.stream().collect(Collectors.toMap(
                WithdrawStrategy::getType, a -> a));
    }
    /**
     * 根据类型获取策略类
     */
    public static WithdrawStrategy getInstance(Integer type) {
        return strategyMap.get(type);
    }
}
